package com.ad.platform.service.impl;

import com.ad.payload.dto.PfAdvertisementDto;
import com.ad.payload.dto.PfAdvertisementRenewDto;
import com.ad.payload.model.PageView;
import com.ad.payload.vo.PfAdvertisementVo;
import com.ad.platform.common.utils.BeanUtil;
import com.ad.platform.enums.AdPayItemEnum;
import com.ad.platform.mapper.PfAdvertisementMapper;
import com.ad.platform.mapper.PfAdvertisementPayItemMapper;
import com.ad.platform.model.PfAdvertisement;
import com.ad.platform.model.PfAdvertisementPayItem;
import com.ad.platform.service.IPfAdvertisementService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *    广告 服务实现类
 * </p>
 *
 * @author rclia
 * @since 2021-03-17
 */
@Service
@Slf4j
public class PfAdvertisementServiceImpl extends ServiceImpl<PfAdvertisementMapper, PfAdvertisement> implements IPfAdvertisementService {


    @Autowired
    private PfAdvertisementMapper pfAdvertisementMapper;

    @Autowired
    private PfAdvertisementPayItemMapper pfAdvertisementPayItemMapper;

    @Transactional
    @Override
    public boolean saveOrUpdate(@Valid PfAdvertisementDto pfAdvertisementDto) {
        log.info("入参 ============================================ " + JSON.toJSONString(pfAdvertisementDto));

        PfAdvertisement pfAdvertisement = new PfAdvertisement();
        BeanUtils.copyProperties(pfAdvertisementDto,pfAdvertisement);

        if(pfAdvertisementDto.getId() == null){
            //费用项目
            PfAdvertisementPayItem pfAdvertisementPayItem = new PfAdvertisementPayItem();
            //计算广告生效日期 到期日期
            LocalDateTime date= LocalDateTime.now();
            pfAdvertisement.setAdEffectiveDate(date);
            pfAdvertisement.setAdExpireDate(date.plusDays(getPayItemDay(AdPayItemEnum.D.getCode())));
            //新增广告主表
            pfAdvertisementMapper.insert(pfAdvertisement);
            //新增广告子表
            pfAdvertisementPayItem.setRefAdId(pfAdvertisement.getId());
            pfAdvertisementPayItem.setPayItem(pfAdvertisementDto.getPayItem());
            pfAdvertisementPayItem.setEffectiveDate(date);
            pfAdvertisementPayItem.setExpireDate(pfAdvertisement.getAdExpireDate());
            pfAdvertisementPayItemMapper.insert(pfAdvertisementPayItem);
        } else {
            //更新
            //pfCategoryMapper.updateById(category);
        }

        return true;
    }

    private Integer getPayItemDay(String payItem){
        if(AdPayItemEnum.D.getCode().equals(payItem)){
            return AdPayItemEnum.D.getValue();
        } else if(AdPayItemEnum.C.getCode().equals(payItem)){
            return AdPayItemEnum.C.getValue();
        } else if(AdPayItemEnum.B.getCode().equals(payItem)){
            return AdPayItemEnum.B.getValue();
        } else {
            return AdPayItemEnum.A.getValue();
        }
    }

    @Override
    public PageView<PfAdvertisementVo> selectPage(long pageNo, long pageSize) {
        IPage<PfAdvertisement> page = new Page(pageNo, pageSize);
        IPage<PfAdvertisement> pfAdvertisementIPage = pfAdvertisementMapper.selectPfAdvertisementPage(page, null);

        // 通过Ipage实例化pageView
        PageView pageView = new PageView(pfAdvertisementIPage);

        // 实体转vo
        List<PfAdvertisementVo> rtnList = new ArrayList();
        pfAdvertisementIPage.getRecords().forEach(item -> {
            PfAdvertisementVo vo = new PfAdvertisementVo();
            BeanUtil.copyProperties(item, vo);
            rtnList.add(vo);
        });

        pageView.setPageList(rtnList);
        return pageView;
    }

    @Transactional
    @Override
    public boolean removeById(@NotNull(message = "id不能为空") Long id) {
        log.info("入参 ============================================  " + id);
        pfAdvertisementMapper.deleteById(id);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ref_ad_id",id);
        pfAdvertisementPayItemMapper.deleteByMap(params);
        return true;
    }

    @Transactional
    @Override
    public boolean adRenew(@Valid PfAdvertisementRenewDto pfAdvertisementRenewDto) {
        log.info("入参 ============================================  " + pfAdvertisementRenewDto.getId());
        log.info("入参 ============================================  " + pfAdvertisementRenewDto.getPayItem());

        //查询当前广告
        PfAdvertisement advertisement = pfAdvertisementMapper.selectById(pfAdvertisementRenewDto.getId());
        //判断当前广告是否到期
        LocalDateTime nowTime= LocalDateTime.now();
        if(nowTime.isAfter(advertisement.getAdExpireDate())){
            //广告已经过期(设置广告新的过期日期)
            advertisement.setAdExpireDate(nowTime.plusDays(getPayItemDay(pfAdvertisementRenewDto.getPayItem())));
            pfAdvertisementMapper.updateById(advertisement);
            //新增广告子表
            PfAdvertisementPayItem pfAdvertisementPayItem = new PfAdvertisementPayItem();
            pfAdvertisementPayItem.setRefAdId(pfAdvertisementRenewDto.getId());
            pfAdvertisementPayItem.setPayItem(pfAdvertisementRenewDto.getPayItem());
            pfAdvertisementPayItem.setEffectiveDate(nowTime);
            pfAdvertisementPayItem.setExpireDate(advertisement.getAdExpireDate());
            pfAdvertisementPayItemMapper.insert(pfAdvertisementPayItem);
        } else {
            //新增广告子表
            PfAdvertisementPayItem pfAdvertisementPayItem = new PfAdvertisementPayItem();
            pfAdvertisementPayItem.setRefAdId(pfAdvertisementRenewDto.getId());
            pfAdvertisementPayItem.setPayItem(pfAdvertisementRenewDto.getPayItem());
            pfAdvertisementPayItem.setEffectiveDate(advertisement.getAdExpireDate());
            pfAdvertisementPayItem.setExpireDate(advertisement.getAdExpireDate().plusDays(getPayItemDay(pfAdvertisementRenewDto.getPayItem())));
            pfAdvertisementPayItemMapper.insert(pfAdvertisementPayItem);
            //广告已经过期(设置广告新的过期日期)
            advertisement.setAdExpireDate(advertisement.getAdExpireDate().plusDays(getPayItemDay(pfAdvertisementRenewDto.getPayItem())));
            pfAdvertisementMapper.updateById(advertisement);
        }
        return true;
    }

    @Override
    public List<PfAdvertisementVo> randomListPfAdvertisement() {

        List<PfAdvertisement> adList =  pfAdvertisementMapper.randomSelectLimit8();

        // 实体转vo
        List<PfAdvertisementVo> rtnList = new ArrayList();
        adList.forEach(item -> {
            PfAdvertisementVo vo = new PfAdvertisementVo();
            BeanUtil.copyProperties(item, vo);
            rtnList.add(vo);
        });

        return rtnList;
    }


}
